package com.zft.userwalletmanager.web.controller;

import com.zft.userwalletmanager.application.handler.AccountFlowHandler;
import com.zft.userwalletmanager.domain.bo.WalletBO;
import com.zft.userwalletmanager.infrastructure.common.utils.DateFormatUtil;
import com.zft.userwalletmanager.infrastructure.common.utils.ValidationUtils;
import com.zft.userwalletmanager.infrastructure.gateway.database.model.AccountFlowDO;
import com.zft.userwalletmanager.web.Result;
import com.zft.userwalletmanager.web.request.AccountFlowDetailsRequest;
import com.zft.userwalletmanager.web.response.AccountFlowDetailsResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/accountFlow")
public class AccountFlowController {
    @Autowired
    private AccountFlowHandler accountFlowHandler;

    /**
     * 查询用户钱包金额变动明细
     * @param accountFlowDetailsRequest
     * @return
     */
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Result<List<AccountFlowDetailsResponse>> changeDetails(@RequestBody AccountFlowDetailsRequest accountFlowDetailsRequest) {
        ValidationUtils.validate(accountFlowDetailsRequest);
        WalletBO walletBO = new WalletBO();
        walletBO.setUserId(accountFlowDetailsRequest.getUserId());
        List<AccountFlowDO> accountFlowDOS = accountFlowHandler.selectAccountFlowDetails(walletBO);
        List<AccountFlowDetailsResponse> accountFlowDetailsResponses = new ArrayList<>();
        for (AccountFlowDO accountFlowDO: accountFlowDOS) {
            AccountFlowDetailsResponse accountFlowDetailsResponse = new AccountFlowDetailsResponse();
            accountFlowDetailsResponse.setOrderId(accountFlowDO.getOrderId());
            accountFlowDetailsResponse.setType(accountFlowDO.getType());
            accountFlowDetailsResponse.setAmount(accountFlowDO.getAmount());
            accountFlowDetailsResponse.setCashFlow(accountFlowDO.getCashFlow());
            accountFlowDetailsResponse.setCounterPartyWalletNo(accountFlowDO.getCounterPartyWalletNo());
            accountFlowDetailsResponse.setCreateTime(DateFormatUtil.dateFormat(accountFlowDO.getCreateTime()));
            accountFlowDetailsResponses.add(accountFlowDetailsResponse);
        }
        return Result.success(accountFlowDetailsResponses);
    }
}
